Tilesets in OpenGL

OpenGL makes it difficult to have mipmaps for tileset textures with non-2n tile sizes or tile count. It requires that each mipmap is sized half the size of its parent, rounded downwards. But with tilesets, normal mipmapping will bleed pixel values from adjacent tiles into each other if we follow this rule naively; so, instead, we have to do some trickery.

Consider a tileset with a cell size of 5x5 pixels. If we create a mipmap, we want each cell of the mipmap to have 2x2 pixels. But in a tileset of 3x3 cells (15x15 pixels total), the mipmap is required by OpenGL to be 7x7 pixels, whereas we would want our mipmap to be 3x3 cells of 2x2 pixels, or 6x6 pixels overall, to prevent any colour bleeding between cells.

We can solve this by either extending the texture or rearranging the tiles in the texture so that the number of tiles in each dimension is a multiple of 2n, where n is the number of mipmaps we want to produce. Rearranging the tiles avoids wasted memory, but can be more complex to implement, as the program using the texture does not know exactly how it was rearranged, hence a tile coordinate translation has to be added.